package com.dmall.chapter03.section03;

/**
 * 加缓存的方式：自底向上的遍历，空间复杂度上优化为O(1)
 * 时间复杂度：O(n)
 * 空间复杂度：O(1)
 * @author xiao1.wang
 * @date 2020-12-01 10:54:21
 */
public class Leetcode70V4 {

    public int climbStairs(int n) {
        if (n <= 3) {
            return n;
        }
        int cache1 = 1;
        int cache2 = 2;
        int cache3 = 0;
        for (int i = 3; i <= n; i++) {
            cache3 = cache2 + cache1;
            cache1 = cache2;
            cache2 = cache3;
        }
        return cache3;
    }

    public static void main(String[] args) {
        Leetcode70V4 leetcode70 = new Leetcode70V4();
        System.out.println(leetcode70.climbStairs(8));
    }
}
